home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Amiga Format CD 43
/
Amiga Format CD43 (1999)(Future Publishing)(GB)(Track 1 of 2)[!][issue 1999-09].iso
/
-serious-
/
hardware
/
psx-memcard_tool
/
src
/
psx.asc
< prev
Wrap
Text File
|
1999-06-14
|
18KB
|
804 lines
' This is AMOS source code in ascii-format
Parallel Open
' Just blank buffer screen for icons
Unpack 4 To 1
Screen Hide 1
' Main screen
Unpack 5 To 0
' Save names
Dim NAMES$(15)
' Little stupid speed test
TIME=0
Timer=0
Repeat
Inc TIME
Until Timer>50
SPEED=TIME/80000
If SPEED>30 Then SPEED=30
' Draw speed meter
Ink 6,6,6
YPOSSE=47+SPEED*2
Polygon 273,YPOSSE-2 To 273,YPOSSE+2 To 278,YPOSSE
TIME=0
IMAGE=1
Do
' Every tenth cycle = icon anim update
Add UPDAT,1,0 To 9
' Set active screen
Screen 0
' Read keyboard if esc is pressed and free resources
KB$=Inkey$
SC=Scancode
If SC=69
Parallel Close
For ER=1 To 3
Erase ER
Next ER
End
End If
KB$=""
' Get mouse coordinates
COORDX=X Mouse-128
COORDY=Y Mouse-46
' TIME = little delay if gadgets are pressed
If TIME>0
Dec TIME
End If
' Zoom if right mouse button is pressed
If Mouse Key=2
Zoom 0,48,169,66,187 To 0,72,153,108,189
End If
' Read left mouse button
If(Mouse Key=1 and TIME=0)
' Speed adjustment part
If(COORDX>272 and COORDX<314)
If(COORDY>31 and COORDY<45)
Dec SPEED
If SPEED<0
SPEED=0
End If
End If
If(COORDY>109 and COORDY<123)
Inc SPEED
If SPEED>30
SPEED=30
End If
End If
' Clear old speed triangle
Ink 0,0,0
Bar 271,43 To 280,110
' Draw new speed triangle
Ink 6,6,6
YPOSSE=47+SPEED*2
Polygon 273,YPOSSE-2 To 273,YPOSSE+2 To 278,YPOSSE
Wait 5
End If
If(COORDX>22 and COORDX<100)
' Download
If(COORDY>21 and COORDY<33)
' Change gadget color
Ink 3
Paint 23,20
Ink 1
Paint 24,22
' Card read sub part
Gosub CARD_READ
' Update icons
Gosub MAKEICONS
' Set delay
TIME=10
' Restore gadget
Ink 7
Paint 23,20
Ink 5
Paint 24,22
End If
' Upload
If(COORDY>41 and COORDY<53)
Ink 3
Paint 23,40
Ink 1
Paint 24,42
' Card write sub part
Gosub CARD_WRITE
TIME=10
Ink 7
Paint 23,40
Ink 5
Paint 24,42
End If
' Undelete
If(COORDY>61 and COORDY<73 and Length(3)>0)
Ink 3
Paint 23,60
Ink 1
Paint 24,62
' Mark saves in use
Gosub UNDEL
' Update icons
Gosub MAKEICONS
TIME=10
Ink 7
Paint 23,60
Ink 5
Paint 24,62
End If
' Format
If(COORDY>81 and COORDY<93)
Ink 3
Paint 23,80
Ink 1
Paint 24,82
' Fill whole memorycard with zeros
Gosub FOHMAT
TIME=10
Ink 7
Paint 23,80
Ink 5
Paint 24,82
End If
' Load
If(COORDY>111 and COORDY<123)
Ink 3
Paint 23,110
Ink 1
Paint 24,112
' Load sub part
Gosub FILE_LOAD
' If file was loaded
If Length(3)>0
Gosub MAKEICONS
End If
TIME=10
Ink 7
Paint 23,110
Ink 5
Paint 24,112
End If
' Save
If(COORDY>131 and COORDY<143)
Ink 3
Paint 23,130
Ink 1
Paint 24,132
' If there is data in memory
If Length(3)>0
Gosub FILE_SAVE
End If
TIME=10
Ink 7
Paint 23,130
Ink 5
Paint 24,132
End If
' Change icon
If(COORDX<37 and COORDY>161 and COORDY<173)
Ink 3
Paint 23,160
Ink 1
Paint 24,162
If IMAGE>1
Dec IMAGE
Gosub ICON
End If
TIME=4
Ink 7
Paint 23,160
Ink 5
Paint 24,162
End If
If(COORDX<37 and COORDY>181 and COORDY<193)
Ink 3
Paint 23,180
Ink 1
Paint 24,182
If IMAGE<15
Inc IMAGE
Gosub ICON
End If
TIME=4
Ink 7
Paint 23,180
Ink 5
Paint 24,182
End If
End If
End If
'
If FRAMES>0 and UPDAT=9
Add FRAME,1,1 To FRAMES
Paste Bob 49,170,FRAME
End If
Wait Vbl
Loop
Data "$VER: PSX-MEMCARD TOOL v0.07"
'----------------------------------------------------------
' Change colors 16 to 31, get icons
' IMAGE = current save
ICON:
Screen 0
' Check if current save block is in use & top block
If Peek(Start(3)+IMAGE*$80)=$51
' Icon window to black
Ink 6
Bar 49,170 To 64,185
' Clear zoom area
Ink 0
Bar 72,153 To 108,189
' 16 colors, get and set different components
' Data block +$60 - $7f palette, 16 colors, 16 bits per color
'
' bit 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
' ?? B4 B3 B2 B1 B0 G4 G3 G2 G1 G0 R4 R3 R2 R1 R0
' ------------------ ------------------ ------------------
' Blue data Green data Red data
For CLR=0 To 15
' ARS = Color address
ARS=Start(3)+IMAGE*$2000+$60+CLR*2
' 16 bits, LSB to MSB
WORD=Peek(ARS)+Peek(ARS+1)*$100
' 5 bits per component, drop LSB out
RED=(WORD and $1F)/2
GREEN=(WORD and $3E0)/$40
BLUE=(WORD and $7C00)/$800
Colour 16+CLR,$100*RED+$10*GREEN+BLUE
Next CLR
' Get number of icon frames
' Data block +2 bits 0-3 = icon count
' bits 4-7 = icon display flag
FRAMES=Peek(Start(3)+IMAGE*$2000+2) and $F
' Get blitter objects from buffer screen
For FRAME=1 To FRAMES
Get Bob 1,FRAME,(FRAME-1)*16,(IMAGE-1)*16 To FRAME*16,IMAGE*16
Next FRAME
' Output save name to bottom of screen
Ink 0,0
Bar 48,191 To 319,199
Ink 6,0
Text 48,198,Left$(NAMES$(IMAGE),32)
Else
' v0.06 bug fixed, occured if 1st block wasn't in use
FRAMES=0
End If
Return
'----------------------------------------------------------
' Get savenames and make icons to buffer
'
MAKEICONS:
' Clear text window area
Screen 0
Ink 4,4
Bar 121,31 To 269,179
' 15 save blocks
For IM=1 To 15
' Clear old name
NAMES$(IM)=""
' Check if block is in use
If Peek(Start(3)+IM*$80)=$51
' ARS = Current block address
ARS=Start(3)+IM*$2000
' Make filename
N=0
Repeat
' One character = 16 bits
WORD=Deek(ARS+4+2*N)
If WORD<>0
CLR=(WORD and $FF00)/$100
CHR=WORD and $FF
' SPC = put space if char is not supported
SPC=True
If CLR=$82
' a..z
If(CHR>$80 and CHR<$9B)
SPC=0
NAMES$(IM)=NAMES$(IM)+Chr$(CHR-$20)
End If
' A..Z
If(CHR>$5F and CHR<$7A)
SPC=0
NAMES$(IM)=NAMES$(IM)+Chr$(CHR-$1F)
End If
' 0..9
If(CHR>$4E and CHR<$59)
SPC=0
NAMES$(IM)=NAMES$(IM)+Chr$(CHR-$1F)
End If
End If
If CLR=$81
' «
If CHR=$E1
SPC=0
NAMES$(IM)=NAMES$(IM)+Chr$($AB)
End If
' »
If CHR=$E2
SPC=0
NAMES$(IM)=NAMES$(IM)+Chr$($BB)
End If
' other character from table (bank 6)
If(CHR>$3F and CHR<$99)
SPC=0
NAMES$(IM)=NAMES$(IM)+Chr$(Peek(Start(6)+CHR-$40))
End If
End If
' put space if char wasn't supported
If SPC
NAMES$(IM)=NAMES$(IM)+" "
End If
End If
Inc N
' 0 = end of name, (N just if data is broken)
Until N>31 or WORD=0
' Output 18 chars from name to text area
Screen 0
Ink 6,4
Text 123,28+IM*10,Left$(NAMES$(IM),18)
' Make icon image
Screen 1
' Data block +2 bits 0-3 = icon count
FRAMES=Peek(ARS+2) and $F
For FRAME=1 To FRAMES
' Data block + icon*$80 = start of icon pixel data
' 4 bits per pixel, one icon = $80 bytes
ARS=ARS+$80
For YP=0 To 15
For XP=0 To 7
WORD=Peek(ARS+YP*8+XP)
CLR=WORD and $F
Plot(FRAME-1)*16+XP*2,(IM-1)*16+YP,CLR+16
CLR=(WORD and $F0)/$10
Plot(FRAME-1)*16+XP*2+1,(IM-1)*16+YP,CLR+16
Next XP
Next YP
Next FRAME
End If
Next IM
' Set current image to 1 and draw icon
IMAGE=1
Gosub ICON
Return
'----------------------------------------------------------
' Load memcard file to memory bank 3
FILE_LOAD:
' Requester
FILE$=Fsel$("","","Load PSX-memcard file")
' If filename was chosen
If FILE$<>""
' Clear GOOD and BAD indicators
Ink 6
Paint 121,24
Paint 184,24
' Reserve memory, loading a file bigger than 140000 bytes causes problems
Erase 3
Reserve As Data 3,140000
' Binary load
Bload FILE$,Start(3)
' .gme format file
' Cut $F40 bytes from start
If Leek(Start(3))=$3132332D
Reserve As Work 2,132000
Copy Start(3)+$F40,Start(3)+$20F40 To Start(2)
Copy Start(2),Start(2)+$20000 To Start(3)
Erase 2
End If
' .psx format file
If(Instr(Lower$(FILE$),".psx")>0) and Not(Deek(Start(3))=$4D43)
Reserve As Work 2,132000
' Copy empty directory block in the start of work buffer
Copy Start(7),Start(7)+$1E000 To Start(2)
' Modify directory block
N=0
' Put game id
Repeat
BYTE=Peek(Start(3)+N)
Poke Start(2)+$8A+N,BYTE
Inc N
Until BYTE=0 or N>50
' Calculate xor flag
BYTE=0
For N=$80 To $FE
BYTE=BYTE xor Peek(Start(2)+N)
Next N
Poke Start(2)+$FF,BYTE
Copy Start(3)+$36,Start(3)+$2036 To Start(2)+$2000
' Check if .psx-file was a .psx file
If Not(Deek(Start(3)+$36)=$5343)
Doke Start(2),0
End If
Copy Start(2),Start(2)+$20000 To Start(3)
Erase 2
End If
' If file was bad or good
If Not(Deek(Start(3))=$4D43)
Ink 15
Paint 184,24
Else
Ink 8
Paint 121,24
End If
End If
Return
'----------------------------------------------------------
' Save whole memorycard file
FILE_SAVE:
' Requester
FILE$=Fsel$("","","Save raw PSX-memcard file")
' If name was chosen
If FILE$<>""
Bsave FILE$,Start(3) To Start(3)+$20000
End If
Return
'----------------------------------------------------------
' Read memorycard via hardware and parallelport
CARD_READ:
' Clear position indicator, GOOD&BAD indicators
Ink 4,4,4
Bar 32,13 To 287,15
Ink 6
Paint 121,24
Paint 184,24
' No errors
ERR=0
' Reserve memory
Erase 3
Reserve As Data 3,132000
' ADRS = byte position
ADRS=0
' SAV = save bits from hardware to memory
SAV=False
' Parallel data bits
' I designed hardware to be pc-parallel port combatible (except getting power),
' standard pc-parallel port is very stupid, designed only for printing..
' D0 => command 0=0,1=1
' D1 => clock, H\L edge action
' D2 => select, 0=active
' BUSY <= data in 0=0,1=1
' POUT <= acknowledge 0=active, not used yet, should be 0 after every 8 bit
' D0,D1,D2 output
Poke $BFE301,7
' BUSY input
Bclr 0,$BFD200
' POUT input
Bclr 1,$BFD200
' Whole memorycard = 1024 times 128 byte block
' It's not hard to use only required blocks, I'm just lazy...
For N=0 To 1023
' CHCK = check read end flag ('G'=good, 'N'=not good)
CHCK=False
' Outputs to high
Poke $BFE101,$FF
' Select memorycard
Bclr 1,$BFE101
' C=command
' Command header 4 bytes
C=$81 : Gosub SUB_READ
C=$52 : Gosub SUB_READ
C=0 : Gosub SUB_READ
C=0 : Gosub SUB_READ
' Read address, $000..$3FF
C=N/256 : Gosub SUB_READ
C=N and $FF : Gosub SUB_READ
' Command acknowledge
C=0 : Gosub SUB_READ
' Data header
C=0 : Gosub SUB_READ
' Data address
C=0 : Gosub SUB_READ
C=0 : Gosub SUB_READ
' Data block, 128 bytes, save to memory
For B=0 To 127
C=0 : SAV=True : Gosub SUB_READ
Next B
' xor code
C=0 : Gosub SUB_READ
' Read end flag
C=0 : CHCK=True : Gosub SUB_READ
' If not good
If RET<>$47 : Inc ERR : End If
' Draw position indicator
Ink 15-(N/128)
Draw 32+N/4,13 To 32+N/4,15
Next N
' GOOD/BAD thing
If ERR>0
Ink 15
Paint 184,24
Else
Ink 8
Paint 121,24
End If
Return
' 8 bits per command
SUB_READ:
For BIT=0 To 7
' Output CMD bit
If Btst(BIT,C)
Bset 0,$BFE101
Else
Bclr 0,$BFE101
End If
' CLK=0
Bclr 2,$BFE101
' Simple stupid delay
For DELAY=0 To SPEED : Next DELAY
' Save DAT to memory
If SAV
If Btst(0,$BFD000)
Bset BIT,Start(3)+ADRS
Else
Bclr BIT,Start(3)+ADRS
End If
End If
' Read end flag check
If CHCK
If Btst(0,$BFD000)
Bset BIT,RET
Else
Bclr BIT,RET
End If
End If
' CLK=1
Bset 2,$BFE101
' Simple stupid delay
For DELAY=0 To SPEED : Next DELAY
Next BIT
' Increase current byte address
If SAV
Inc ADRS
End If
SAV=False
Return
'----------------------------------------------------------
' Write data to memorycard via hardware and parallelport
CARD_WRITE:
' Clear position indicator, GOOD&BAD indicators
Ink 4,4,4
Bar 32,13 To 287,15
Ink 6
Paint 121,24
Paint 184,24
ERR=0
DAT_OFFSET=0
' Enable output signals
Poke $BFE301,7
' Enable input signals
' Data in
Bclr $BFD200,0
' Acknowledge in
Bclr $BFD200,1
' 1024 128byte blocks
For N=0 To 1023
' Output high
Poke $BFE101,$FF
' Select
Bclr 1,$BFE101
' Command header
C=$81 : Gosub WRITE_SUB
C=$57 : Gosub WRITE_SUB
C=0 : Gosub WRITE_SUB
C=0 : Gosub WRITE_SUB
' Write address
' X = xor code
C=N/256 : Gosub WRITE_SUB
X=C
C=N and $FF : Gosub WRITE_SUB
X=C xor X
' Data bytes
For B=0 To 127
C=Peek(Start(3)+DAT_OFFSET) : Gosub WRITE_SUB
X=C xor X
Inc DAT_OFFSET
Next B
' Send xor code
C=X : Gosub WRITE_SUB
' Write end mark
C=0 : Gosub WRITE_SUB
C=0 : Gosub WRITE_SUB
' Write end flag
C=0 : Gosub WRITE_SUB
' Good?
If RET<>$47 : Inc ERR : End If
' Position indicator
Ink 15-(N/128)
Draw 32+N/4,13 To 32+N/4,15
Next N
If ERR>0
Ink 15
Paint 184,24
Else
Ink 8
Paint 121,24
End If
Return
WRITE_SUB:
' 8 bits
For BIT=0 To 7
' Send CMD bit
If Btst(BIT,C)
Bset 0,$BFE101
Else
Bclr 0,$BFE101
End If
' CLK = 0
Bclr 2,$BFE101
' Simple stupid delay
For DELAY=0 To SPEED : Next DELAY
' Get DAT bit
If Btst(0,$BFD000)
Bset BIT,RET
Else
Bclr BIT,RET
End If
' CLK = 1
Bset 2,$BFE101
For DELAY=0 To SPEED : Next DELAY
Next BIT
Return
'----------------------------------------------------------
UNDEL:
' Marks deleted files 'busy'
' * directory frame
' +00 data flag
' bit 7-4 A free(not use , delete)
' 5 busy
' F reserve
' bit 3-0 0 not use
' 1 top block
' 2 link block
' 3 link end block
' F reserve
For N=1 To 15
ADRS=N*$80+Start(3)
BYTE=Peek(ADRS)
If(BYTE and $F0)=$A0
Poke ADRS,BYTE xor $F0
BYTE=Peek(ADRS+$7F)
Poke ADRS,BYTE xor $F0
End If
Next N
Gosub CARD_WRITE
Return
'----------------------------------------------------------
' Fill memory card with zeros
' Sometimes when you cannot use saveblocks in a PSX,
' this will probably help..
FOHMAT:
Ink 4,4,4
Bar 32,13 To 287,15
Ink 6
Paint 121,24
Paint 184,24
ERR=0
' Enable output signals
Poke $BFE301,7
' Enable input signals
' Data in
Bclr $BFD200,0
' Acknowledge in
Bclr $BFD200,1
' 1024 138byte transfers
For N=0 To 1023
Poke $BFE101,$FF
Bclr 1,$BFE101
C=$81 : Gosub WRITE_SUB
C=$57 : Gosub WRITE_SUB
C=0 : Gosub WRITE_SUB
C=0 : Gosub WRITE_SUB
C=N/256 : Gosub WRITE_SUB
X=C
C=N and $FF : Gosub WRITE_SUB
X=C xor X
For B=0 To 127
C=0 : Gosub WRITE_SUB
X=C xor X
Inc DAT_OFFSET
Next B
C=X : Gosub WRITE_SUB
C=0 : Gosub WRITE_SUB
C=0 : Gosub WRITE_SUB
C=0 : Gosub WRITE_SUB
If RET<>$47 : Inc ERR : End If
Ink 15-(N/128)
Draw 32+N/4,13 To 32+N/4,15
Next N
If ERR>0
Ink 15
Paint 184,24
Else
Ink 8
Paint 121,24
End If
Return